package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.health.dto.SportDTO;
import io.renren.modules.health.dto.SportPlanDTO;
import io.renren.modules.health.dto.SportPlanDetailDTO;
import io.renren.modules.health.service.SportPlanDetailService;
import io.renren.modules.health.service.SportPlanService;
import io.renren.modules.health.service.SportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 饮食方案
 */
@RestController
@RequestMapping("api/sportplan")
@Api(tags="饮食方案")
public class SportPlanController {
    @Autowired
    private SportPlanService sportPlanService;
    @Autowired
    private SportPlanDetailService sportPlanDetailService;
    @Autowired
    private SportService sportService;

    @GetMapping("list")
    @ApiOperation("查询运动计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cusId", value = "客户id", paramType = "query", required = true, dataType="int")})
    @Login
    public Result<List<SportPlanDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        sportPlanService.genCustomerSportPlan(Long.parseLong((String) params.get("cusId")));
        List<SportPlanDTO> sportPlanDTOS = sportPlanService.querySportPlanWithDetail(Long.parseLong((String) params.get("cusId")));
        return new Result<List<SportPlanDTO>>().ok(sportPlanDTOS);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @Login
    public Result<SportPlanDTO> get(@PathVariable("id") Long id){
        SportPlanDTO data = sportPlanService.get(id);
        HashMap<String, Object> param = new HashMap<>();
        param.put("sportPlanId",id);
        List<SportPlanDetailDTO> sportPlanDetailDTOS = sportPlanDetailService.list(param);
        param.put("sportIds",sportPlanDetailDTOS.stream().map(SportPlanDetailDTO::getSportId).distinct().collect(Collectors.toList()));
        data.setSportPlanDetails(sportPlanDetailDTOS);
        List<SportDTO> foodDTOS = sportService.list(param);
        sportPlanDetailDTOS.forEach(sportPlanDetail -> sportPlanDetail.setSportName(
                foodDTOS.stream().filter(food -> sportPlanDetail.getSportId().equals(food.getId()))
                        .findFirst()
                        .orElse(new SportDTO()).getSportName()));
        return new Result<SportPlanDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @Login
    public Result save(@RequestBody SportPlanDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        sportPlanService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @Login
    public Result update(@RequestBody SportPlanDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sportPlanService.update(dto);

        return new Result();
    }

}